import { apiGetUserInfo } from '@/api/apis.js'
import { login, logout } from '@/api/auth.ts'
import {
  setToken,
  removeToken,
  setUserId,
  setUserName,
  removeUserId,
  removeUserName,
} from '@/utils/auth'
import defAva from '@/common/images/profile.jpg'
import { defineStore } from 'pinia'
import { toast } from '@/utils/common'

const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      token: '',
      accountId: '',
      name: '',
      avatar: '',
      role: '',
      remainingDownloads: 0,
      downloadSize: 0,
      scoreSize: 0,
      publishSize: 0,
      collectionSize: 0,
    }),
    actions: {
      // 登录
      login(userInfo) {
        const username = userInfo.username.trim()
        const password = userInfo.password
        const code = userInfo.code
        const uuid = userInfo.uuid
        return new Promise((resolve, reject) => {
          login(username, password, code, uuid)
            .then((res) => {
              // console.log(res)
              this.token = res.token
              setToken(res.token)
              // 登录后获取用户信息
              this.getInfo()
                .then((res) => {
                  toast('登录成功')
                  uni.switchTab({
                    url: '/pages/index/index',
                  })
                  resolve(res)
                })
                .catch((error) => {
                  toast(error.message)
                  reject(error)
                })
            })
            .catch((error) => {
              toast(error.message)
              reject(error)
            })
        })
      },
      // 获取用户信息
      getInfo() {
        return new Promise((resolve, reject) => {
          apiGetUserInfo()
            .then((res) => {
              if (res.code === 200) {
                this.setUserInfo(res.data)
                toast('获取用户信息成功')
                resolve(res)
              } else {
                reject(res.message)
              }
              resolve(res)
            })
            .catch((error) => {
              reject(error)
            })
        })
      },
      setUserInfo(user) {
        this.accountId = user.accountId
        this.name = user.nickname
        this.role = user.role
        this.avatar = user.avatar ? user.avatar : defAva
        this.remainingDownloads = user.remainingDownloads
        this.downloadSize = user.downloadSize
        this.scoreSize = user.scoreSize
        this.publishSize = user.publishSize
        this.collectionSize = user.collectionSize
        setUserId(user.accountId)
        setUserName(user.nickname)
      },
      resetUserInfo() {
        this.accountId = ''
        this.name = ''
        this.role = ''
        this.avatar = ''
        this.remainingDownloads = 0
        this.downloadSize = 0
        this.scoreSize = 0
        this.publishSize = 0
        this.collectionSize = 0
      },
      getUserInfo() {
        return {
          accountId: this.accountId,
          name: this.name,
          role: this.role,
          avatar: this.avatar,
          remainingDownloads: this.remainingDownloads,
          downloadSize: this.downloadSize,
          scoreSize: this.scoreSize,
          publishSize: this.publishSize,
          collectionSize: this.collectionSize,
        }
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          logout(this.token)
            .then(() => {
              this.token = ''
              uni.clearStorageSync()
              this.resetUserInfo()
              toast('退出成功')
              resolve()
            })
            .catch((error) => {
              console.log(error)
              reject(error)
            })
        })
      },
    },
  },
  {
    persist: true,
  },
)

export default useUserStore
